<div class="system-messages">
    <ul>
        <?php
        $messages = $this->messages;
        foreach ($messages as $message) :
            ?>
            <li><?= $message ?></li>
        <?php
        endforeach
        ?>
    </ul>
</div>
<div class="admin-search-n-filter">
    <div class="admin-search">
        <label><input type="text" value="<?= $this->enteredKeyWords ?>"/></label>
        <input type="button" value=""/>
    </div>
    <div class="admin-hyperlink-btn">
        <a href="/classico/public/admin/user/index">Refresh</a>
    </div>
    <div class="admin-filter">
        <label>
            <select>
                <option value="0">-- User Type --</option>
                <?php
                /** @var UserTypePOJO[] $userTypePOJOs */
                $userTypePOJOs = $this->userTypePOJOs;
                foreach ($userTypePOJOs as $userTypePOJO) :
                    if ($this->selectedUserTypeID &&
                        $userTypePOJO->ID == $this->selectedUserTypeID
                    ) :
                        ?>
                        <option value="<?= $userTypePOJO->ID ?>"
                                selected="selected">
                            <?= $userTypePOJO->Name?>
                        </option>
                    <?php
                    endif;
                    if (!$this->selectedUserTypeID ||
                        $userTypePOJO->ID != $this->selectedUserTypeID
                    ) :
                        ?>
                        <option value="<?= $userTypePOJO->ID ?>">
                            <?= $userTypePOJO->Name ?>
                        </option>
                    <?php
                    endif;
                endforeach
                ?>
            </select>
        </label>
    </div>
</div>
<div class="datagrid">
    <table>
        <tr>
            <th width="2%"><label><input id="check-box-all" type="checkbox"/></label></th>
            <th width="2%">ID</th>
            <th width="2%">Username</th>
            <th width="10%">Email</th>
            <th width="15%">RealName</th>
            <th>Birthday</th>
            <th>MobilePhone</th>
            <th>CreatedDate</th>
            <th width="5%" class="center">Confirm</th>
            <th width="5%" class="center">Enabled</th>
            <th width="8%">UserType</th>
            <th width="5%" class="center">Delete</th>
        </tr>
        <?php
        /** @var UserPOJO[] $users */
        $users = $this->users;
        foreach ($users as $index => $user) :
            ?>
            <tr class="<?= ($index % 2 == 0) ? 'alt' : '' ?>">
                <td>
                    <label>
                        <input class="check-box-user-id" type="checkbox"
                               value="<?= $user['ID'] ?>"/>
                    </label>
                </td>
                <td><?= $user['ID'] ?></td>
                <td>
                    <a href="/classico/public/admin/user/update/userID/<?= $user['ID'] ?>">
                        <?= $user['Username'] ?>
                    </a>
                </td>
                <td><?= $user['Email'] ?></td>
                <td><?= $user['RealName'] ?></td>
                <td><?= $user['Birthday'] ?></td>
                <td><?= $user['MobilePhone'] ?></td>
                <td><?= $user['CreatedDate'] ?></td>
                <td class="center">
                    <?php
                    if (!$user['Confirmed']) :
                        ?>
                        <div class="enabled-block-icon tool-icon"></div>
                        <form action="/classico/public/admin/user/confirm" method="post">
                            <input type="hidden" name="isPost" value=""/>
                            <input type="hidden" name="userIDs[]" value="<?= $user['ID'] ?>"/>
                        </form>
                    <?php
                    endif
                    ?>
                    <?php
                    if ($user['Confirmed']) :
                        ?>
                        <div class="enabled-confirm-icon tool-icon"></div>
                        <form action="/classico/public/admin/user/unconfirm" method="post">
                            <input type="hidden" name="isPost" value=""/>
                            <input type="hidden" name="userID" value="<?= $user['ID'] ?>"/>
                        </form>
                    <?php
                    endif
                    ?>
                </td>
                <td class="center">
                    <?php
                    if (!$user['Enabled']) :
                        ?>
                        <div class="enabled-block-icon tool-icon"></div>
                        <form action="/classico/public/admin/user/unblock" method="post">
                            <input type="hidden" name="isPost" value=""/>
                            <input type="hidden" name="userIDs[]" value="<?= $user['ID'] ?>"/>
                        </form>
                    <?php
                    endif
                    ?>
                    <?php
                    if ($user['Enabled']) :
                        ?>
                        <div class="enabled-unblock-icon tool-icon"></div>
                        <form action="/classico/public/admin/user/block" method="post">
                            <input type="hidden" name="isPost" value=""/>
                            <input type="hidden" name="userIDs[]" value="<?= $user['ID'] ?>"/>
                        </form>
                    <?php
                    endif
                    ?>
                </td>
                <td><?= $user['UserType'] ?></td>
                <td class="center">
                    <div class="enabled-delete-icon tool-icon"></div>
                    <form action="/classico/public/admin/user/delete" method="post">
                        <input type="hidden" name="isPost" value=""/>
                        <input type="hidden" name="userIDs[]" value="<?= $user['ID'] ?>"/>
                    </form>
                </td>
            </tr>
        <?php
        endforeach
        ?>
    </table>
</div>
<?= $this->paginationControl($this->paginator, 'Sliding', 'my_pagination_control.phtml') ?>
<script type="text/javascript">
    function forwardHyperlink() {
        var currentURL = window.location.href;

        var forwardURL = currentURL.replace(/\/user\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/index\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/keyWords\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/userTypeID\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/page\/[\w\W]*/, '');

        forwardURL += '/user/index';

        var keyWords = $('.admin-search input[type = "text"]').val();
        if (keyWords != '') {
            forwardURL += '/keyWords/' + keyWords;
        }

        var userTypeID = $('.admin-filter select').val();
        if (userTypeID != 0) {
            forwardURL += '/userTypeID/' + userTypeID;
        }

        window.location.href = forwardURL;
    }

    $('.admin-search input[type = "text"]').keydown(function (event) {
        if (event.which == 13) {
            forwardHyperlink();
        }
    });

    $('.admin-search input[type = "button"]').click(function () {
        forwardHyperlink();
    });

    $('.admin-filter select').change(function () {
        forwardHyperlink();
    });
</script>
<script type="text/javascript">
    $('#check-box-all').click(function () {
        if ($(this).attr('checked')) {
            $('.check-box-user-id').attr('checked', true);
        } else {
            $('.check-box-user-id').attr('checked', false);
        }
    });

    $('.datagrid .tool-icon').click(function () {
        $(this).parent(null).find('form').submit();
    });
</script>
<script type="text/javascript">
    function process() {
        var form = $(this).find('form');

        var checkedCheckBoxes = $('.check-box-user-id:checked');
        for (var i = 0; i < checkedCheckBoxes.length; i++) {
            var userID = checkedCheckBoxes[i].value;
            var tag = '<input type="hidden" name="userIDs[]" value="' + userID + '"/>';
            form.append(tag);
        }

        form.submit();
    }

    $('#tool-btn-delete.enabled-tool-btn').click(process);
    $('#tool-btn-update.enabled-tool-btn').click(function () {
        var checkedCheckBoxes = $('.check-box-user-id:checked');
        var userID = checkedCheckBoxes[0].value;
        location.href = '/classico/public/admin/user/update/userID/' + userID;
    });
    $('#tool-btn-confirm.enabled-tool-btn').click(process);
    $('#tool-btn-unblock.enabled-tool-btn').click(process);
    $('#tool-btn-block.enabled-tool-btn').click(process);
</script>